<?php
require_once("Resource.php");
require_once("function.php");
class ResourceDB
{
	//初始化DB
	function InitDB()
	{  
		$db = MySQL_connect("localhost", "root"); 
		MySQL_select_db("player",$db); 
		mysql_query("set names utf8");
	}

	//添加资源
	function AddResource($name1,$name2)
	{
		$this->InitDB();
		$guid=create_guid();
		$date_time=date("Y-m-d H:i:s");
		$sql = "INSERT INTO resource (ID,name1,name2,createtime) VALUES ('$guid','$name1','$name2','$date_time')";               
		$result = MySQL_query($sql);  
	}
	
	//删除资源
	function DeleteResource($Id)
	{
		$this->InitDB();
		$sql = "delete from resource where Id='".$Id."'";             
		$result = MySQL_query($sql);  
	}
	//通过Id选择
	function SelectById($Id)
	{
		$this->InitDB();
		$sql = "select Id,Name1,Name2,CreateTime from  resource where Id='".$Id."'";  
		$result = MySQL_query($sql);  
		$resource=null;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$resource=new Resource();
				$resource->Id=$row["Id"];
				$resource->Name1=$row["Name1"];
				$resource->Name2=$row["Name2"];
				$resource->CreateTime=$row["CreateTime"];
				break;
			}
		return $resource;
	}

	//选择所有
	function SelectAll()
	{
		$this->InitDB();
		$sql = "select Id,Name1,Name2,CreateTime from  resource order by createtime desc";  
		$result = MySQL_query($sql);  
		$lstResource=array();
		$i=0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$resource=new Resource();
				$resource->Id=$row["Id"];
				$resource->Name1=$row["Name1"];
				$resource->Name2=$row["Name2"];
				$resource->CreateTime=$row["CreateTime"];
				$lstResource[$i]=$resource;
				$i++;
			}
		return $lstResource;
	}
}


?>